/*
 * 生成html
 * @author li.xiaoxi
 * @date 2016-12-22
 */

module.exports = function(env){
  //依赖模块
  var path = require('path')
  var glob = require('glob')
  var HtmlWebpackPlugin = require('html-webpack-plugin')

  //全局配置
  var global_config = global.think_config,
      webpack_config = global_config.webpack;

  var cwd = process.cwd(),
      url = path.resolve(cwd, global_config.init.app, webpack_config.htmlRule),
      files = glob.sync(url),
      plugins = [];

  if(files && files.length) {
    files.forEach(function(file_path){
      var file_name = file_path.substring(file_path.lastIndexOf('\/') + 1,
        file_path.length);

        var opts = {
          filename: file_name,
          template: file_path,
          chunks: [file_name.split('.html')[0]+'_entry'],
          // files: {
          //   js: [jsPath+filename.split('.')[0]+'_entry.js']
          // },
          inject: true,
          hash: webpack_config.hash
        };

        if(webpack_config.build.htmlminify) {
          opts.minify = {
              removeComments: true,
              collapseWhitespace: true,
              removeAttributeQuotes: true
              // more options:
              // https://github.com/kangax/html-minifier#options-quick-reference
            };
        }else {
          opts.minify = false;
        }

        plugins.push(
          new HtmlWebpackPlugin(opts)
        )
    });
  }

  return plugins;
}
